package com.otp.controller;

import com.otp.pojo.dto.AnnouncementDto;
import com.otp.pojo.entity.Announcement;
import com.otp.pojo.request.announcement.*;
import com.otp.util.ResponseMessage;
import com.otp.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    @GetMapping
    public ResponseMessage<List<AnnouncementDto>> list() {
        return ResponseMessage.success(announcementService.list()
                .stream()
                .map(Announcement::convert2Dto)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<AnnouncementDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(announcementService.getById(id)
                .convert2Dto());
    }

    @PostMapping
    public ResponseMessage<?> create(@RequestBody AnnouncementCreateRequest request) {
        return ResponseMessage.success(announcementService.create(request)
                .convert2Dto());
    }

    @PutMapping
    public ResponseMessage<?> update(@RequestBody AnnouncementUpdateRequest request) {
        return ResponseMessage.success(announcementService.update(request)
                .convert2Dto());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<?> delete(@PathVariable Long id) {
        announcementService.delete(id);
        return ResponseMessage.success(null);
    }
}